package top.glkj.hms.controller.customer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.glkj.hms.entity.VipUser;
import top.glkj.hms.entity.User;
import top.glkj.hms.exception.DefineException;
import top.glkj.hms.service.UserService;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2021/1/7 23:51
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer")
public class CustomerController {
    private final UserService userService;

    @Autowired
    public CustomerController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("获取所有客户")
    @GetMapping
    public Result getAllVipUser() throws DefineException {
        return userService.getAllVipUser();
    }
    @ApiOperation("分页获取所有客户")
    @GetMapping("{size}/{page}")
    public Result getAllVipUserByPageAndSize(@PathVariable("page") int page, @PathVariable("size")int size) throws DefineException {
        Result result = userService.getAllVipUserByPageAndSize(page, size);

         return result;
    }
    @ApiOperation("根据用户id获取所有客户")
    @GetMapping("{id}")
    public Result getVipUser(@PathVariable("id") long id) throws DefineException {
        Result one = userService.getOne(id);
        if (ResultFactory.isSuccess(one)){
            User object = (User) one.getObject();
            return userService.getVipUserByUser(object);
        }else {
            return one;
        }
    }
    @ApiOperation("添加客户")
    @PostMapping
    public Result addVipUser(VipUser vipUser) throws DefineException {
        return userService.addVipUser(vipUser);
    }
    @ApiOperation("修改客户")
    @PutMapping
    public Result updateVipUser(VipUser vipUser) throws DefineException {
        return userService.updateVipUser(vipUser);
    }
    @ApiOperation("移除客户")
    @DeleteMapping("{id}")
    public Result removeVipUser(@PathVariable("id") long id) throws DefineException {
        return userService.removeVipUser(new VipUser(id));
    }
    @ApiOperation("删除客户")
    @DeleteMapping("/delete/{id}")
    public Result deleteVipUser(@PathVariable("id") long id) throws DefineException {
        return userService.deleteVipUser(new VipUser(id));
    }
}
